package com.ytktt.study.week8.task6.hmily.tcc.order.controller;

import com.ytktt.study.week8.task6.hmily.tcc.order.service.OrderService;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.math.BigDecimal;

/**
 * TODO
 *
 * @author ytk
 */
@RestController
@RequestMapping("/order")
@RequiredArgsConstructor
public class OrderController {
    
    private final OrderService orderService;

    @PostMapping(value = "/orderPay")
    public String orderPay(@RequestParam(value = "count") Integer count,
                           @RequestParam(value = "amount") BigDecimal amount) {
        final long start = System.currentTimeMillis();
        this.orderService.orderPay(count, amount);
        System.out.println("消耗时间为:" + (System.currentTimeMillis() - start));
        return "";
    }

    @PostMapping(value = "/testOrderPay")
    public String testOrderPay(@RequestParam(value = "count") Integer count,
                               @RequestParam(value = "amount") BigDecimal amount) {
        final long start = System.currentTimeMillis();
        this.orderService.testOrderPay(count, amount);
        System.out.println("消耗时间为:" + (System.currentTimeMillis() - start));
        return "";
    }

    @PostMapping(value = "/mockItemStockWithTryException")
    public String mockItemStockWithTryException(@RequestParam(value = "count") Integer count,
                                                @RequestParam(value = "amount") BigDecimal amount) {
        return this.orderService.mockItemStockWithTryException(count, amount);
    }

    @PostMapping(value = "/mockItemStockWithTryTimeout")
    public String mockItemStockWithTryTimeout(@RequestParam(value = "count") Integer count,
                                              @RequestParam(value = "amount") BigDecimal amount) {
        return this.orderService.mockItemStockWithTryTimeout(count, amount);
    }

    @PostMapping(value = "/mockAccountWithTryException")
    public String mockAccountWithTryException(@RequestParam(value = "count") Integer count,
                                              @RequestParam(value = "amount") BigDecimal amount) {
        return this.orderService.mockAccountWithTryException(count, amount);
    }

    @PostMapping(value = "/mockAccountWithTryTimeout")
    public String mockAccountWithTryTimeout(@RequestParam(value = "count") Integer count,
                                            @RequestParam(value = "amount") BigDecimal amount) {
        return this.orderService.mockAccountWithTryTimeout(count, amount);
    }

    @PostMapping(value = "/orderPayWithNested")
    public String orderPayWithNested(@RequestParam(value = "count") Integer count,
                                     @RequestParam(value = "amount") BigDecimal amount) {
        return this.orderService.orderPayWithNested(count, amount);
    }

    @PostMapping(value = "/orderPayWithNestedException")
    public String orderPayWithNestedException(@RequestParam(value = "count") Integer count,
                                              @RequestParam(value = "amount") BigDecimal amount) {
        return this.orderService.orderPayWithNestedException(count, amount);
    }

    @PostMapping(value = "/mockItemStockWithConfirmTimeout")
    public String mockItemStockWithConfirmTimeout(@RequestParam(value = "count") Integer count,
                                                  @RequestParam(value = "amount") BigDecimal amount) {
        return this.orderService.mockItemStockWithConfirmTimeout(count,amount);
    }
}
